<template>
  <div class="login-container">
    <van-nav-bar title="登录" left-arrow @click-left="$router.go(-1)" />
    <van-form @submit="onSubmit" ref="form">
      <van-field
        v-model="user.mobile"
        icon-prefix="toutiao"
        left-icon="shouji"
        maxlength="11"
        name="mobile"
        type="number"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="user.code"
        icon-prefix="toutiao"
        left-icon="yanzhengma"
        maxlength="6"
        type="number"
        name="code"
        placeholder="请输入验证码"
        :rules="rules.code"
      >
        <template #button>
          <van-button
            v-if="isShow"
            size="small"
            type="primary"
            native-type="button"
          >
            <van-count-down :time="5000" format="ss 秒 " @change="changeFn" />
          </van-button>
          <van-button
            v-else
            size="small"
            type="primary"
            native-type="button"
            @click="sendMs"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { sendSmsApi, userLogin } from "@/api/Login";
import { mapMutations } from "vuex";
export default {
  methods: {
    ...mapMutations(["setUser"]),
    //登录表单提交
    async onSubmit() {
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true, //forbidClick	是否禁止背景点击
      });
      try {
        const { data } = await userLogin(this.user);
        this.setUser(data.data);
        console.log(data);
        //如果是被拦截过来到登录界面,登陆成功之后去之前想去的页面,不是则去我的界面
        // console.log(this.$route);
        const url = this.$route.query.url;
        // console.log(url);
        if (url) {
          this.$router.replace(url);
        } else {
          this.$router.push("/my");
        }
        // this.$store.commit("setUser", data.data);
      } catch (error) {
        console.log(error);
      }
      this.$toast.clear();
    },
    //方式一
    // sendMs() {
    //   // this.isShow = true;
    //   this.$refs.form
    //     .validate("mobile")
    //     .then((res) => {
    //       console.log("校验通过");
    //       this.isShow = true;
    //     })
    //     .catch((err) => console.log(err));
    // },
    //方式二
    async sendMs() {
      try {
        await this.$refs.form.validate("mobile");
        console.log("校验通过");
      } catch (error) {
        // console.log(error);
        return;
      }
      this.isShow = true;
      try {
        const res = await sendSmsApi(this.user.mobile);
        // console.log(res);
        this.$toast.success("发送成功");
      } catch (error) {
        this.$toast.fail("发送失败");
      }
    },
    changeFn(time) {
      // console.log(time);
      const { seconds } = time;
      if (seconds === 0) {
        this.isShow = false;
      }
    },
  },
  data() {
    return {
      isShow: false,
      user: {
        mobile: "",
        code: "",
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号" },
          { pattern: /^1[3|4|5|6|7|8|9]|d{9}$/, message: "手机号不正确" },
        ],
        code: [
          { required: true, message: "请输入验证码" },
          { pattern: /\d{6}$/, message: "验证码不正确" },
        ],
      },
    };
  },
};
</script>

<style lang="less" scoped>
.login-container {
  .van-nav-bar {
    background-color: #5094f3;
  }
  /deep/.van-nav-bar__title {
    color: #fff;
  }
  /deep/.van-icon {
    color: #fff;
  }
  .van-count-down {
    color: #fff;
  }
}
</style>
